package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class StockContentDisplayServlet extends HttpServlet {

    private EJBDatabaseQuery ejbdb;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
    {
	doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
    {
        response.setContentType("text/html");
	HttpSession session  = request.getSession(true);
        PrintWriter out      = response.getWriter();
	String user_ip       = request.getRemoteAddr();

	//////////////////////////////////////////////////////////////////////////
	// System resource
	//////////////////////////////////////////////////////////////////////////
	ResourceBundle SystemInfo = (ResourceBundle)session.getAttribute("SystemInfo");
	
	if (SystemInfo == null) {
	    Locale locale = request.getLocale();
	    SystemInfo = ResourceBundle.getBundle("SystemInfo", locale); 
	    session.setAttribute("SystemInfo", SystemInfo);
	}
		
	String local_ips = SystemInfo.getString("3wcenter.local_ips");
	String database  = SystemInfo.getString("3wcenter.database");

	String mid = (String)request.getAttribute("mid");
	if(mid == null) {
	    System.out.println("StockHotListServlet:Error:102");
	    return;
	}

	String call_path  =  request.getContextPath()+request.getServletPath();
	String action     =  request.getParameter("aid");
	String table_name =  request.getParameter("table_name");
	String sub_dir    =  request.getParameter("sub_dir");

	Hashtable dir_hs = new Hashtable();
	dir_hs = (Hashtable)session.getAttribute("ContralOpenClose");
	    
	if(dir_hs == null) {
	    return;
	}

	if(ejbdb == null) {
	    ejbdb= (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	}

	String user_name   = null;
	int user_age       = 0;
	int user_level     = 0;
	String user_fname  = null;
	String user_since  = null;
	String user_edu    = null;
	String user_zip    = null;
	String user_sex    = null;

	Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	if(user_info == null) {
	    System.out.println("ContentDisplayServlet:Error:101");
	    return;
	}

	user_name   = (String)user_info.get("user_name");
	user_age    = Integer.parseInt((String)user_info.get("user_age"));
	user_level  = Integer.parseInt((String)user_info.get("user_level"));
	user_fname  = (String)user_info.get("user_fname");
	user_since  = (String)user_info.get("user_since");
	user_edu    = (String)user_info.get("user_edu");
	user_zip    = (String)user_info.get("user_zip");
	user_sex    = (String)user_info.get("user_sex");

	String query = null;

	if(action == null) {	
	    String  page_id  = null;
	    String  max_page = null;
	    boolean update_top_frame = false;
	    String all_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	    page_id = request.getParameter("pid");

	    if(page_id == null || page_id.length()==0) {
		page_id ="1";
	    }

	    String letter = all_alpha.substring(Integer.parseInt(page_id)-1, Integer.parseInt(page_id));
	    query = "SELECT DISTINCT TICK, SID FROM STOCK_SID WHERE TICK LIKE '"+letter+"%' ORDER BY TICK";
	    ArrayList alist = new ArrayList();
	    alist = ejbdb.select(query);

	    StringBuffer sb = new StringBuffer();
	    // page counter
	    sb.append(MyHtml.abcPageCounter(page_id, call_path+"?mid="+mid));

	    // Sording
	    //sb.append("<BR>AAAAAAAAA");
	    //sb.append("<BR>BBBBBBBBB");

	    // subdir info
	    sb.append((new ServletBean()).getSubdirInfo(request));

	    sb.append("\n<TABLE cellSpacing='2' cellPadding='2' border='0' width='100%'>");
	
	    String stock_tick = null;
	    String stock_sid  = null;
	    String stock_url  = null;

	    ArrayList a = new ArrayList();
	    Iterator it = alist.iterator();
	    int m=0;
	    int t=0;
	    while (it.hasNext()) {
		a = (ArrayList)it.next();
		stock_tick = (String)a.get(0);
		if(stock_tick.length()>5) {
		    continue;
		}
		stock_sid  = (String)a.get(1);
		if(m==0) {
		    sb.append("<TR><TD style='BORDER: 1px solid #669999;'>");
		    sb.append("<A href='"+call_path+"?mid="+mid+"&aid=1&tick="+stock_tick+"&pid="+page_id+"'>");
		    sb.append(stock_tick+"</A>");
		    sb.append("</TD>");
		}
		else {
		    sb.append("<TD style='BORDER: 1px solid #669999;'>");
		    sb.append("<A href='"+call_path+"?mid="+mid+"&aid=1&tick="+stock_tick+"&pid="+page_id+"'>");
		    sb.append(stock_tick+"</A>");
		    sb.append("</TD>");
		}
		m++;
		if(m==8){
		    sb.append("</TR>");
		    m=0;
		}
		t++;
	    }

	    if(m%8 !=0) {
		for(int n=0; n<(8-m);n++) {
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		}
		sb.append("</TR>");
	    }

	    if(t/8<20) {
		for(int n=0; n<(20-t/8);n++) {
		    sb.append("<TR>");
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		    sb.append("<TD style='BORDER: 1px solid #669999;'>&nbsp;</TD>");
		    sb.append("</TR>");
		}
	    }

	    sb.append("\n</TABLE>");

	    out.println(sb);
	    return;
	}

	if(action.equals("1")) { // display chart
	    String back_page_id = request.getParameter("pid");
	    String tick = request.getParameter("tick");
	    String tick_sid = ejbdb.selectSingleFieldValue("SELECT  DISTINCT SID FROM STOCK_SID WHERE TICK='"+tick+"';","SID");
	    String sector = "n/a";
	    String volume_link = call_path+"?mid="+mid+"&aid=2&tick="+tick;

	    GenerateStockChart gc = new GenerateStockChart();
	    out.println(gc.getChart(tick, tick_sid, sector, volume_link));
	    return;
	}
	
	if(action.equals("2")) { //volume
	    String tick = request.getParameter("tick");
	    out.println((new StockVolumeBean()).getVolume(tick));
	    return;
	}
    }
}






